package me.zhenqu.model.bo.impl;

import me.zhenqu.model.bo.StatusBo;
import me.zhenqu.model.dao.StatusCountDao;
import me.zhenqu.model.dao.StatusDao;
import me.zhenqu.model.pojo.entity.Status;
import me.zhenqu.model.pojo.entity.StatusCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-10
 * Time: 下午4:56
 * To change this template use File | Settings | File Templates.
 */
@Service("statusBo")
public class StatusBoImpl implements StatusBo {

    @Autowired
    StatusDao statusDao;

    @Autowired
    StatusCountDao statusCountDao;

    public void setStatusDao(StatusDao statusDao) {
        this.statusDao = statusDao;
    }

    public void setStatusCountDao(StatusCountDao statusCountDao) {
        this.statusCountDao = statusCountDao;
    }

    @Override
    public void save(Status status) {
        statusDao.save(status);
    }

    @Override
    public void update(Status status) {
        statusDao.update(status);
    }

    @Override
    public void delete(Status status) {
        statusDao.delete(status);
        deleteCount(status.getStatus_id());
        if (status.getIn_reply_status_id()>0)
            delete1StatusCount(status);
    }

    private void deleteCount(long status_id) {
        StatusCount sc = statusCountDao.findByID(status_id);
        if (sc!=null)
            statusCountDao.delete(sc);
    }

    private void delete1StatusCount(Status status) {
        long status_id = status.getIn_reply_status_id();
        boolean comment = status.isIs_comment();
        StatusCount count = getStatusCount(status_id);
        if (count==null)
            return;
        if (comment){
            int comment_count = count.getComment_count()-1;
            if (comment_count<0)
                comment_count = 0;
            count.setComment_count(comment_count);
        }else{
            int retweet_count = count.getRetweet_count()-1;
            if (retweet_count<0)
                retweet_count = 0;
            count.setRetweet_count(retweet_count);
        }
        count.setUpdate_time(System.currentTimeMillis());
        statusCountDao.update(count);
    }

    @Override
    public Status findByID(Long user_id) {
        return statusDao.findByID(user_id);
    }

    @Override
    public List<Status> findUserStatusSince(Long user_id, long since) {
        return statusDao.findUserStatusSince(user_id, since);
    }

    @Override
    public List<Status> findCommentStatusSince(Status status, long since) {
        return statusDao.findCommentStatusSince(status, since);
    }

    @Override
    public StatusCount getStatusCount(Long status_id) {
        return statusCountDao.findByID(status_id);
    }

    public void add1StatusCount(Long status_id, boolean comment) {
        StatusCount count = getStatusCount(status_id);
        boolean newob = false;
        if (count==null){
            count = new StatusCount();
            newob = true;
            count.setStatus_id(status_id);
        }
        if (comment)
            count.setComment_count(count.getComment_count()+1);
        else
            count.setRetweet_count(count.getRetweet_count()+1);
        count.setUpdate_time(System.currentTimeMillis());
        if (newob)
            statusCountDao.save(count);
        else
            statusCountDao.update(count);
    }
}
